<template>
  <div class="left-menu">
    <div class="logo">
      <router-link to="/">
        <img src="http://oss.waperidea.cn/A9/WDCUI_logo.png" alt="" />
      </router-link>
    </div>
    <div v-for="(item, i) in menuList" :key="i">
      <div v-if="item.title" class="title">{{ item.title }}</div>
      <router-link v-else class="link" :to="item.path">{{
        item.docName
      }}</router-link>
      <div class="sub-menu">
        <template v-if="item.child">
          <router-link
            v-for="link in item.child"
            :key="link.path"
            class="link"
            :to="link.path"
            >{{ link.name }}</router-link
          >
        </template>
      </div>
    </div>
  </div>
</template>

<script>
import { routes } from "../../router";
export default {
  data() {
    return {
      routes,
    };
  },
  methods: {},
  computed: {
    menuList() {
      let obj = {};
      this.routes.map((item) => {
        // 二级链接
        if (item.meta?.title) {
          obj[item.meta.title] ??= [];
          obj[item.meta.title].push({
            name: item.meta.docName,
            path: item.path,
          });
        } else {
          // 一级链接
          obj[item.name] = {
            name: item.name,
            path: item.path,
            docName: item.meta.docName,
          };
        }
      });
      const result = Object.keys(obj).map((item) => {
        if (Array.isArray(obj[item])) {
          return {
            title: item,
            child: obj[item],
          };
        }
        return obj[item];
      });
      return result;
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.left-menu {
  box-sizing: border-box;
  //   border-right: 1px solid #ccc;
  min-height: 100vh;
  .title {
    font-size: 12px;
    color: #999;
    line-height: 26px;
    margin-top: 15px;
  }
  .link {
    display: block;
    height: 40px;
    color: #444;
    line-height: 40px;
    font-size: 16px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-weight: 400;
    text-decoration: none;
    &:hover {
        color: #4389f9;
    }
    &.router-link-exact-active{
        color: #4389f9;
    }
  }
  .sub-menu {
    .link {
      padding-left: 10px;
    }
  }
  .logo {
    display: flex;
    align-items: center;
    justify-content: space-around;
    padding: 20px 0;
    border-bottom: 1px solid #ccc;
    img {
      width: 80px;
    }
  }
}
</style>